//
//  ZHLEEStatusToolBarView.swift
//  ZHLEE_Blog_Swift_Pro
//
//  Created by ZHLEEBlander on 15/10/17.
//  Copyright © 2015年 ZHLEEBlander. All rights reserved.
//

import UIKit

class ZHLEEStatusToolBarView: UIView {

// MARK: - 
// MARK: //-----------------属性--------------------------
 /// Array:存放toolbar的按钮
    private lazy var buttons:[UIButton] = [UIButton]()
 /// Array:分割线数组
    private lazy var splites:[UIImageView] = [UIImageView]()
    
    //转发
    var repostsButton: UIButton?
    //评论
    var commentsButton: UIButton?
    //点赞
    var attitudesButton: UIButton?
    
    var status: ZHLEEStatus?{
    
        didSet{
        
            setStatus()
        }
    }
    
    
// MARK: - 
// MARK: //-----------------方法--------------------------

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        repostsButton = addButton("timeline_icon_retweet", title: "转发")
        commentsButton = addButton("timeline_icon_comment", title: "评论")
        attitudesButton = addButton("timeline_icon_unlike", title: "赞")
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setStatus(){
    
        setCount(repostsButton!, count: status!.reposts_count, defalutTitle: "转发")
        setCount(commentsButton!, count: status!.comments_count, defalutTitle: "评论")
        setCount(attitudesButton!, count: status!.attitudes_count, defalutTitle: "赞")
    }
    
    private func setCount(button: UIButton,count: Int,defalutTitle:String){
    
        if count == 0{
            button.setTitle(defalutTitle, forState: UIControlState.Normal)
        }else{
        
            if count < 10000{
            
                button.setTitle("\(count)", forState: UIControlState.Normal)
            }else{
            
                let result = count / 1000
                var resultStr = "\(CGFloat(result) / CGFloat(10))万"
                
                resultStr = resultStr.stringByReplacingOccurrencesOfString(".0", withString: "")
                
                button.setTitle(resultStr, forState: UIControlState.Normal)
            }
        }
    }
    /**
    设置button
    
    - parameter imageName: <#imageName description#>
    - parameter title:     <#title description#>
    */
    private func addButton(imageName:String,title:String) -> UIButton{
    
        let bgImageName = "timeline_card_bottom_background"
        
        let button  = UIButton.button(title: title,titleFontFloat:CGFloat(15),titleColor: RGB(r: 80, g: 80, b: 80), image: imageName, bgImage: bgImageName,  target: self, action: "buttonclick")
        addSubview(button)
        buttons.append(button)
        return button
    }
    
    @objc private func buttonclick(){
    
        printLog("Home tool Bar Button Click")
        
    }
    /**
    添加分割线
    */
    private func addSpliteView(){
    
        let imageView = UIImageView(image: UIImage(named: "timeline_card_bottom_line"))
        addSubview(imageView)
        splites.append(imageView)
    }
    /**
    layoutSubviews
    */
    override func layoutSubviews() {
        super.layoutSubviews()
        
        //按钮宽度
        let buttonW = ScreenW / CGFloat(buttons.count)
        
        for i in 0..<buttons.count {
        
            let btn = buttons[i]
            
            btn.size = CGSizeMake(buttonW, height)
            
            btn.x = CGFloat(i) * buttonW
        }
        
        for i in 0..<splites.count {
        
            let view = splites[i]
            
            view.centerX = buttonW * CGFloat(i + 1)
        }
    }
}
